<?php

/**
 * LEAGUEMan
 * 
 * Copyright (C) 2007 Marko Bischof
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_VALID_MOS') or die('Direct Access to this location is not allowed.');

if (!isset($project) && !($project instanceof LM_Table_Project)) {

    if ($task == 'open') {
        
        $id = intval(mosGetParam($_REQUEST, 'id', $cid[0]));
        $row = LM::getLMTable("Teammap");
        if (!$row->load(intval($id))) {
            LM::redirect("project", "No such Team available!");
        }

        LM::setProject($row->teammap_project);
        LM::setTeam($row->teammap_id);
        LM::redirect("playermap");
    }

    LM::redirect();
}

switch ($task) {

    case 'open':
        $id = intval(mosGetParam($_REQUEST, 'id', $cid[0]));
        LM::setTeam($id);
        LM::redirect("playermap");
        break;
    
    case 'toggleOptionShowResults':

        $row = LM::getLMTable("Teammap");
        if (!$row->load(intval($cid[0]))) {
            LM::redirect("teammap", "No such Team available!");
        }

        $row->toggleOption('showResults');
        LM::redirect("teammap");

        break;

    case 'toggleOptionShowTable':

        $row = LM::getLMTable("Teammap");
        if (!$row->load(intval($cid[0]))) {
            LM::redirect("teammap", "No such Team available!");
        }

        $row->toggleOption('showTable');
        LM::redirect("teammap");

        break;

    case 'toggleOptionShowSquad':

        $row = LM::getLMTable("Teammap");
        if (!$row->load(intval($cid[0]))) {
            LM::redirect("teammap", "No such Team available!");
        }

        $row->toggleOption('showSquad');
        LM::redirect("teammap");

        break;

    case 'toggleOptionShowHistory':

        $row = LM::getLMTable("Teammap");
        if (!$row->load(intval($cid[0]))) {
            LM::redirect("teammap", "No such Team available!");
        }

        $row->toggleOption('showHistory');
        LM::redirect("teammap");

        break;

    case 'toggleOptionShowClub':

        $row = LM::getLMTable("Teammap");
        if (!$row->load(intval($cid[0]))) {
            LM::redirect("teammap", "No such Team available!");
        }

        $row->toggleOption('showClub');
        LM::redirect("teammap");

        break;

   case 'toggleOptionShowNews':

        $row = LM::getLMTable("Teammap");
        if (!$row->load(intval($cid[0]))) {
            LM::redirect("teammap", "No such Team available!");
        }

        $row->toggleOption('showNews');
        LM::redirect("teammap");

        break;

    case 'unassign':

        if (!$project->isAdmin()) {
            LM::redirect("teammap", "You are not authorized for this action");
        }

        $total = count($cid);

        $i = 0;
        foreach ($cid AS $id) {

            $teammap = LM::getLMTable("Teammap");
            $teammap->load($id);

            $matchtable = LM::getLMTable("Match");
            $matchtable->filterbyProject($project->getID());
            $matchtable->filterbyTeam($teammap->teammap_team);
            $matches = $matchtable->getTotalRows();

            if ($matches == 0 && $teammap->delete()) {
                $i++;
            }
        }

        LM::redirect("teammap", "$i Teams unassigned");

        break;

    case 'save':
        $row = LM::getLMTable("Teammap");
        $row->save($_POST);

        $image = saveImage($mainframe->getCfg("absolute_path") . "/images/stories/leagueman/teams/", "upload", "tm{$row->getID()}");

        if ($image) {
            $row->teammap_image = $image;
        }

        if ($row->store()) {
            $msg = "OK";
        } else {
            $msg = "Could not save Team";
        }

        LM::redirect("teammap", $msg);
        break;

    case 'cancel':
        LM::redirect("teammap");
        break;

    case 'edit':
        $row = LM::getLMTable("Teammap");
        $row->load($cid[0]);

        $t = LM::getLMTable("Team");
        $t->load($row->teammap_team);

        $users[] = mosHTML::makeOption('0', 'Select User');
        $database->setQuery("SELECT id AS value, name AS text FROM #__users ");
        $users = array_merge($users, $database->loadObjectList());

        $template = LM::getTemplate();
        $template->assign("view", "admin/form_teammap.tpl.php");
        $template->assign("project", $project);
        $template->assign("team", $team);
        $template->assign("my", $my);
        $template->assign("row", $row);
        $template->assign("t", $t);
        $template->assign("users", $users);

        $template->display("admin/view.tpl.php");
        break;

    default:

        $project_id = $database->Quote(intval($project->getID()));

        $query = "SELECT tm.*, t.*, COUNT(pm.playermap_id) as players, u.username as admin
                    FROM #__leagueman_teammap tm
              INNER JOIN #__leagueman_team t on tm.teammap_team = t.team_id
               LEFT JOIN #__users u on tm.teammap_user = u.id
               LEFT JOIN #__leagueman_playermap pm ON pm.playermap_project = $project_id AND pm.playermap_team = tm.teammap_team
                   WHERE tm.teammap_project = $project_id
                GROUP BY tm.teammap_id";

        $database->setQuery($query);
        $rows = $database->loadObjectList();

        $template = LM::getTemplate();
        $template->assign("view", "admin/list_teammap.tpl.php");
        $template->assign('project', $project);
        $template->assign('team', $team);
        $template->assign('act', $act);
        $template->assign('my', $my);
        $template->assign('rows', $rows);

        $template->display('admin/view.tpl.php');
        break;
}